<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\Banner as BannerModel;
use app\admin\model\Gcategory as GcategoryModel;
use app\admin\model\Goods as GoodsModel;
use app\admin\model\News as NewsModel;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        //获取banner
        $banner = BannerModel::field('image')->order('weigh desc')->select();
        if (empty($banner)) {
            $this->error('未找到轮播图');
        }
        foreach ($banner as &$banner_v) {
            $banner_v['image'] = cdnurl($banner_v['image'], true);
        }
        $return['banner'] = $banner;
        //获取中间记录
        $category = GcategoryModel::field("name,id as cate_id")->order('weigh desc')->select();
        $category_list[] = ['cate_id'=>0,'name'=>'全部'];
        foreach($category as $k => $list)
        {
            $category_list[] = $list;
            
        }

        $return['category_list'] = $category_list;
        //获取商品列表
        $goods_list = GoodsModel::field('id as goods_id,goods_name,pirce as price,good_images,v_pirce as v_price,sell')->order('sort desc')->select();
        foreach ($goods_list as $k => $v) {
            $image_list = explode(',',$v['good_images']);
            $goods_list[$k]['good_images'] = cdnurl($image_list[0], true);

        }
        $return['goods_list'] = $goods_list;
        
        $news_list = NewsModel::field('id as news_id,name,visit,images,creattime')->order('creattime desc')->limit(0,20)->select();
        $return['news_list'] = $news_list;
        
        $this->success('查询成功', $return);
    }
}
